<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of StreamController
 *
 * @author QuangKhoi
 */
class StreamController {
    /**
     * Store model object
     * @var Stream
     */
    private $model;
    
    /**
     * Store view for the stream
     * @var StreamView
     */
    private $view;
    
    /**
     * 
     * @param type $model
     * @param type $view
     */
    public function __construct($model, $view) {
        $this->model = $model;
        $this->view = $view;
    }
    
    /**
     * 
     * @param DateTime $startDate
     * @param DateTime $endDate
     * @return Int
     */
    public function count($startDate, $endDate) {
        $query = $this->model->query;
        $language = $this->model->language;
        $timezone = 7;

        //Language: 2 = vi, 0 = all
        $zMessage = new ZMessageReader($startDate, $endDate, $timezone, $query, $language, 0, 0);
        $collection = $zMessage->read();
        return $collection->getTotal();
    }
    
    /**
     * 
     * @param Int $streamId
     */
    public function render($startDate, $endDate, $index, $length) {
        //Need to edit
        $query = $this->model->query;
        $language = $this->model->language;
        $timezone = 7;

        //Language: 2 = vi, 0 = all
        $zMessage = new ZMessageReader($startDate, $endDate, $timezone, $query, $language, $index, $length);
        $collection = $zMessage->read();
        $signals = array();
        foreach($collection->getItems() as $message) {
            $signal = Signal::withItem($message, $this->model);
            //$signalController->save
            $signals[] = $signal;
        }
        
        $this->view->model = $this->model;
        $this->view->signals = $signals;
        $masterView = MasterView::getInstance();
        $masterView->streamView = $this->view;
    }
    
    public function renderEmail($date, $index, $length) {
        //Need to edit
        $query = $this->model->query;
        $language = $this->model->language;
        $timezone = 7;

        //Language: 2 = vi, 0 = all
        $zMessage = new ZMessageReader($date, $date, $timezone, $query, $language, $index, $length);
        $collection = $zMessage->read();
        $signals = array();
        $items = $collection->getItems();
        for($i = 0; $i < 10; $i++) {
            $signal = Signal::withItem($items[$i], $this->model);
            $signalController = new SignalController($signal, new SignalView());
            //$signalController->save();
            $signals[] = $signal;
        }
        
        $this->view->model = $this->model;
        $this->view->signals = $signals;
        $masterView = MasterView::getInstance();
        $masterView->streamView = $this->view;
    }
    
    public static function getStream($folderId) {
        $userId = $_SESSION['userid'];
        $query = "SELECT * FROM streams WHERE folderid = $folderId AND userid = '$userId'";
        $result = Database::getInstance()->query($query);
        if ($result->returnCode == 200)  //OK
            if ($result->dataTable->numOfRow > 0) {
                $rows = $result->dataTable->rows;
                $streams = array();
                foreach($rows as $row) {
                    $stream = new Stream();
                    $stream->streamId = $row["streamid"];
                    $stream->userId = $row["userid"];
                    $stream->name = $row["name"];
                    $stream->type = $row["type"];
                    $stream->query = $row["query"];
                    $stream->language = $row["language"];
                    $stream->location = $row["location"];
                    $stream->platform = $row["platform"];
                    $stream->externalSources = $row["externalsources"];
                    $stream->folderId = $row["folderid"];
                    $streams[] = $stream;
                }
                return $streams;
            }
            else
                return array();
    }
    
    /**
     * Update folder infos or add new stream to folder
     */
    public function save() {
        $model = $this->model;
        if ($model->streamId !== null) {
        $query = "SELECT * FROM streams WHERE streamid = '$model->streamId'";
        $result = Database::getInstance()->query($query);
        if ($result->returnCode == 200)  //OK
            if ($result->dataTable->numOfRow > 0) {
                Database::getInstance()->query("UPDATE streams
                        SET userid='$model->userId', 
                        name='$model->name', 
                        type='$model->type', 
                        query='$model->query',
                        language='$model->language',
                        location='$model->location',
                        platform='$model->platform',
                        externalsources='$model->externalSources',
                        folderid='$model->folderid' 
                        WHERE streamid = $model->streamId");
                return Stream::withId($model->streamId);
            }
        }
        else {
            $model->streamId = Database::getID("streams", "streamId");
            Database::getInstance()->query("INSERT INTO streams
                        (streamid, userid, name, type, query, language, location,
                         platform, externalsources, folderid, datecreated) VALUES
                        ('$model->streamId', '$model->userId', '$model->name', 
                        '$model->type', '$model->query', 
                        '$model->language', '$model->location', 
                        '$model->platform', '$model->externalSources',
                        '$model->folderId', CURDATE())");
            return Stream::withId(base64_encode($model->streamId));
        }
    }
    
    /**
     * Delete this stream
     */
    public function delete() {
        $model = $this->model;
        $query = "DELETE FROM streams WHERE streamid = $model->streamId";
        Database::getInstance()->query($query);
        return true;
    }
}

?>
